package com.example.fashionshoes.controller.user;

import com.example.fashionshoes.exception.BusinessException;
import com.example.fashionshoes.pojo.po.Coupon;
import com.example.fashionshoes.pojo.po.Result;
import com.example.fashionshoes.service.CouponService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userCouponController")
@RequestMapping("/coupon")
public class CouponController {
    @Resource
    private CouponService couponService;
    /**
     * 优惠劵秒杀
     * @param id 秒杀优惠劵id
     * @return 购买结果
     */
    @PostMapping("/pay")
    public Result<Boolean> payCoupon(@RequestBody Integer id){
        Boolean res;
        try {
            res = couponService.payCoupon(id);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
        return Result.success(res);
    }

    /**
     * 获取商品可用的优惠劵
     * @return 优惠劵集合
     */
    @GetMapping("/get")
    public Result<List<Coupon>> getLimitCoupon(Integer goodsId){
        List<Coupon> couponList = couponService.getLimitCoupon(goodsId);
        return Result.success(couponList);
    }
}
